package com.axsun.service_comments.dao;


import com.axsun.service_comments.entity.Comments;
import com.axsun.service_comments.entity.Info;
import com.axsun.service_comments.entity.Reply;
import com.axsun.service_comments.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.List;

@Mapper
@Repository
public interface UDao {
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "age", column = "age")
    })

    @Select("SELECT * FROM user WHERE age = #{age}")
    List<User> get(int age);

    @Insert("INSERT INTO user(name, age) VALUES (#{name}, #{age})")
    void insert(User user);

    @Insert("INSERT INTO `comments`(content,likes,maintain_id,title) VALUES (#{content}, #{likes}, #{maintain_id}, #{title})")
    void insertComments(Comments comments);

    @Select("SELECT * FROM comments_info")
    List<Info> getAllInfos();

    //    INSERT INTO `comments_info`(id,from_id,comments_id,like_num,content) VALUES('a2', 11,2,12,'fuch')
    @Insert("INSERT INTO `comments_info`(id,from_id,comments_id,like_num,content) VALUES(#{id}, #{from_id},#{comments_id},#{like_num},#{content})")
    void insertInfo(Info info);

    @Select("SELECT * FROM comments_reply")
    List<Reply> getAllReplies();

    //    INSERT INTO `comments_reply`(id,comment_id,from_id,to_id,info_id,content) VALUES(1, 1,1,2,12,'mmmmmm')
    @Insert("INSERT INTO `comments_reply`(id,comment_id,from_id,to_id,info_id,content) VALUES(#{id}, #{comment_id},#{from_id},#{to_id},#{info_id},#{content})")
    void insertReply(Reply reply);

    @Select("SELECT\n" +
            "\tcomments.id,\n" +
            "\tcomments.title,\n" +
            "\tcomments.content,\n" +
            "\tcomments.create_date,\n" +
            "\tcomments.likes,\n" +
            "\tcomments.maintain_id,\n" +
            "\t`user`.`name` \n" +
            "FROM\n" +
            "\tcomments\n" +
            "\tLEFT JOIN `user` ON comments.maintain_id = `user`.id")
    List<Comments> showAllComments();

    @Select("SELECT\n" +
            "\tCOUNT(*) \n" +
            "FROM\n" +
            "\t`comments_info` \n" +
            "WHERE\n" +
            "\tcomments_id =#{key}")
    int getReplyCount(int key);


    @Select("SELECT\n" +
            "\tcomments_info.id,\n" +
            "\tcomments_info.from_id,\n" +
            "\t`user`.`name`,\n" +
            "\tcomments_info.create_time,\n" +
            "\tcomments_info.update_time,\n" +
            "\tcomments_info.like_num,\n" +
            "\tcomments_info.content,\n" +
            "\tcomments_info.comments_id,\n" +
            "\t`user`.age \n" +
            "FROM\n" +
            "\tcomments_info\n" +
            "\tLEFT JOIN `user` ON comments_info.from_id = `user`.id \n" +
            "WHERE\n" +
            "\tcomments_id = #{key}")
    List<Info> ShowSpecInfos(int key);

    @Select("SELECT\n" +
            "\tcomments_reply.id, \n" +
            "\tcomments_reply.from_id, \n" +
            "\tcomments_reply.content, \n" +
            "\t`user`.`name`, \n" +
            "\tcomments_reply.comment_id, \n" +
            "\tcomments_reply.to_id, \n" +
            "\tcomments_reply.info_id, \n" +
            "\tcomments_reply.create_time, \n" +
            "\tcomments_reply.update_time\n" +
            "FROM\n" +
            "\tcomments_reply\n" +
            "\tLEFT JOIN\n" +
            "\t`user`\n" +
            "\tON \n" +
            "\t\tcomments_reply.from_id = `user`.id\n" +
            "WHERE\n" +
            "\tcomment_id = #{comment_id} AND\n" +
            "\tinfo_id = #{info_id}")
    List<Reply> getSpecReplies(int comment_id, int info_id);
}